package com.ruoyi.mes.service.impl;

import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.LnyStoragePut;
import com.ruoyi.mes.domain.PhgRequirement;
import com.ruoyi.mes.domain.PhgRmOrder;
import com.ruoyi.mes.mapper.LnyStoragePutMapper;
import com.ruoyi.mes.mapper.PhgRequirementMapper;
import com.ruoyi.mes.mapper.PhgRmOrderMapper;
import com.ruoyi.mes.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.LnyRmOrderDetailMapper;
import com.ruoyi.mes.domain.LnyRmOrderDetail;
import com.ruoyi.mes.service.ILnyRmOrderDetailService;

/**
 * 采购入库物料详细Service业务层处理
 * 
 * @author coderChen
 * @date 2024-06-16
 */
@Service
public class LnyRmOrderDetailServiceImpl implements ILnyRmOrderDetailService 
{
    @Autowired
    private LnyRmOrderDetailMapper lnyRmOrderDetailMapper;
    @Autowired
    private LnyStoragePutMapper lnyStoragePutMapper;
    @Autowired
    PhgRequirementMapper phgRequirementMapper;
    @Autowired
    PhgRmOrderMapper phgRmOrderMapper;

    /**
     * 查询采购入库物料详细
     * 
     * @param id 采购入库物料详细主键
     * @return 采购入库物料详细
     */
    @Override
    public LnyRmOrderDetail selectLnyRmOrderDetailById(Long id)
    {
        return lnyRmOrderDetailMapper.selectLnyRmOrderDetailById(id);
    }

    /**
     * 查询采购入库物料详细列表
     * 
     * @param lnyRmOrderDetail 采购入库物料详细
     * @return 采购入库物料详细
     */
    @Override
    public List<LnyRmOrderDetail> selectLnyRmOrderDetailList(LnyRmOrderDetail lnyRmOrderDetail)
    {
        return lnyRmOrderDetailMapper.selectLnyRmOrderDetailList(lnyRmOrderDetail);
    }

    /**
     * 新增采购入库物料详细
     * 
     * @param lnyRmOrderDetail 采购入库物料详细
     * @return 结果
     */
    @Override
    public AjaxResult insertPutStorage(List<LnyRmOrderDetail> lnyRmOrderDetail)
    {
        LnyStoragePut lnyStoragePut = new LnyStoragePut();
        BaseController baseController = new BaseController();
        PhgRequirement phgRequirement = new PhgRequirement();
        PhgRmOrder phgRmOrder = new PhgRmOrder();
        String username = baseController.getUsername();
        String storageSn = CodeUtils.storagePutCodeCreat();
        String purchaseSn = lnyRmOrderDetail.get(0).getPurchaseSn();
        // 设置创建者和创建时间
        lnyStoragePut.setCreatedBy(username);
        lnyStoragePut.setCreatedTime(DateUtils.getNowDate());
        lnyStoragePut.setBpsSn(storageSn);
        lnyStoragePut.setBpsType(0);
        lnyStoragePut.setState(0);
        lnyStoragePut.setPurchaseSn(purchaseSn);
        lnyStoragePutMapper.insertLnyStoragePut(lnyStoragePut);
        for(LnyRmOrderDetail lnyRmOrderDetailList : lnyRmOrderDetail){
            lnyRmOrderDetailList.setBpsSn(storageSn);
        }
        lnyRmOrderDetailMapper.insertPutStorage(lnyRmOrderDetail);
        //更改采购需求状态
        for(int i=0;i<lnyRmOrderDetail.size();i++){
            Long proId = lnyRmOrderDetail.get(i).getProId();
            Long orderId = lnyRmOrderDetail.get(i).getOrderId();
            phgRequirement.setState(1);
            phgRequirement.setProId(proId);
            phgRequirement.setOrderId(orderId);
            phgRequirementMapper.updatePhgRequirementState(phgRequirement);
        }
        //更改采购订单到货状态
        for(int i=0;i<lnyRmOrderDetail.size();i++){
            Long proId = lnyRmOrderDetail.get(i).getProId();
            Long orderId = lnyRmOrderDetail.get(i).getOrderId();
            phgRmOrder.setArriveState(1);
            phgRmOrder.setProId(proId);
            phgRmOrder.setOrderId(orderId);
            phgRmOrderMapper.updatePhgRmOrderState(phgRmOrder);
        }
        return AjaxResult.success("申请入库成功");
    }

    /**
     * 修改采购入库物料详细
     * 
     * @param lnyRmOrderDetail 采购入库物料详细
     * @return 结果
     */
    @Override
    public int updateLnyRmOrderDetail(LnyRmOrderDetail lnyRmOrderDetail)
    {
        return lnyRmOrderDetailMapper.updateLnyRmOrderDetail(lnyRmOrderDetail);
    }

    /**
     * 批量删除采购入库物料详细
     * 
     * @param ids 需要删除的采购入库物料详细主键
     * @return 结果
     */
    @Override
    public int deleteLnyRmOrderDetailByIds(Long[] ids)
    {
        return lnyRmOrderDetailMapper.deleteLnyRmOrderDetailByIds(ids);
    }

    /**
     * 删除采购入库物料详细信息
     * 
     * @param id 采购入库物料详细主键
     * @return 结果
     */
    @Override
    public int deleteLnyRmOrderDetailById(Long id)
    {
        return lnyRmOrderDetailMapper.deleteLnyRmOrderDetailById(id);
    }
}
